package pl.squeaky.mvc.nature;

import java.util.ArrayList;
import java.util.Arrays;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;

import pl.squeaky.mvc.builder.MvcBuilder;
import pl.squeaky.mvc.model.IControllers;
import pl.squeaky.mvc.model.IModels;
import pl.squeaky.mvc.model.IViews;

public class MvcNature implements IProjectNature {

	private IProject project;
	
	private IControllers controllers;
	private IModels models;
	private IViews views;
	
	public final static String NATURE_ID = "pl.squeaky.mvc.nature.MvcNature";
		
	@Override
	public void configure() throws CoreException {
		IProjectDescription description = project.getDescription();
		ICommand[] commands = description.getBuildSpec();
		
		for(ICommand command : commands) {
			if(command.getBuilderName().equals(MvcBuilder.BUILDER_ID)) {
				return;
			}
		}
		
		ICommand newCommand = description.newCommand();
		newCommand.setBuilderName(MvcBuilder.BUILDER_ID);
		
		ICommand[] newCommands = Arrays.copyOf(commands, commands.length + 1);
		newCommands[commands.length] = newCommand;
		
		description.setBuildSpec(newCommands);
		project.setDescription(description, null);
		
		//TODO: remove
		System.out.println("configure");
	}

	@Override
	public void deconfigure() throws CoreException {
		
		IProjectDescription description = project.getDescription();
		ICommand[] commands = description.getBuildSpec();
		
		ICommand foundCommand = null;
		for(ICommand command : commands) {
			if(command.getBuilderName().equals(MvcBuilder.BUILDER_ID)) {
				foundCommand = command;
				break;
			}
		}
		
		if(foundCommand == null) {
			return;
		}
		
		ArrayList<ICommand> list = new ArrayList<ICommand>(Arrays.asList(commands));
		list.remove(foundCommand);
		
		ICommand[] newCommands = list.toArray(new ICommand[list.size()]);
		
		description.setBuildSpec(newCommands);
		project.setDescription(description, null);
		
		//TODO: remove
		System.out.println("deconfigure");
	}

	@Override
	public IProject getProject() {
		return this.project;
	}

	@Override
	public void setProject(IProject project) {
		this.project = project;
	}
	
	public void setControllers(IControllers controllers) {
		this.controllers = controllers;
	}
	
	public IControllers getControllers() {
		if(controllers == null) {
			try {
				project.build(IncrementalProjectBuilder.FULL_BUILD, MvcBuilder.BUILDER_ID, null, null);
			} catch (CoreException e) {
				throw new RuntimeException(e);
			}
		}
		return controllers;
	}

	public void setModels(IModels models) {
		this.models = models;
	}
	
	public IModels getModels() {
		if(models == null) {
			try {
				project.build(IncrementalProjectBuilder.FULL_BUILD, MvcBuilder.BUILDER_ID, null, null);
			} catch (CoreException e) {
				throw new RuntimeException(e);
			}
		}
		return models;
	}

	public void setViews(IViews views) {
		this.views = views;
	}
	
	public IViews getViews() {
		if(views == null) {
			try {
				project.build(IncrementalProjectBuilder.FULL_BUILD, MvcBuilder.BUILDER_ID, null, null);
			} catch (CoreException e) {
				throw new RuntimeException(e);
			}
		}
		return views;
	}
}
